package com.winchain.chainbill.tbill.biz.rx;

import com.shell.bee.acct.account.po.AcctBas;
import com.shell.bee.base.exception.UnCheckMsgException;
import com.shell.bee.base.utils.DateUtil;
import com.shell.bee.base.utils.MoneyUtil;
import com.shell.bee.base.utils.StringUtil;
import com.shell.bee.base.utils.SystemUtil;
import com.shell.bee.cfca.CfcaConfiguration;
import com.shell.bee.cfca.paperless.request.CfcaSignRequest;
import com.shell.bee.cfca.paperless.response.BaseResponse;
import com.shell.bee.cfca.paperless.service.CfcaSignTxService;
import com.shell.bee.contract.ContConfiguration;
import com.shell.bee.contract.ContTemplateHelper;
import com.shell.bee.file.po.PubFile;
import com.shell.bee.file.service.PubFileService;
import com.shell.bee.report.utils.FilePathHelper;
import com.shell.bee.report.utils.PdfUtils;
import com.shell.feign.PubAttTmplFeign;
import com.shell.feign.PubUserFeign;
import com.shell.framework.rx.OnErrorNotImplementedSubscriber;
import com.shell.framework.utils.CommonDataUtil;
import com.shell.framework.utils.SysDateUtil;
import com.shell.governor.modules.po.PubAttTmpl;
import com.shell.governor.user.po.PubUser;
import com.winchain.chainbill.ChainBillUtil;
import com.winchain.chainbill.basic.cus.po.CusRec;
import com.winchain.chainbill.basic.cus.service.CusRecService;
import com.winchain.chainbill.tbill.base.po.TbDct;
import com.winchain.chainbill.tbill.base.po.TbRec;
import com.winchain.chainbill.tbill.biz.TBill;
import com.winchain.chainbill.tbill.biz.TBillEvent;
import com.winchain.chainbill.tbill.biz.contract.keyword.ServiceFeeDebitDetailListKeyword;
import com.winchain.chainbill.trade.service.BillAcctService;
import java.io.File;
import java.io.IOException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class TBillSaveSerFeeDebitDetailListSubcriber extends OnErrorNotImplementedSubscriber<TBillEvent<TbDct>> {
	@Autowired
	private PubAttTmplFeign pubAttTmplFeign;
	@Autowired
	private CusRecService cusRecService;
	private static final String DATE_FORMAT = "yyyy��MM��dd��";
	private static final String FILE_PATH = "service-fee";
	private static final String FILE_NAME = "������������";
	private static final String REASON = "����������������";
	@Autowired
	private PubFileService pubFileService;
	@Autowired
	private PubUserFeign pubUserFeign;
	@Autowired
	private CfcaSignTxService cfcaSignTxService;
	@Autowired
	private BillAcctService billAcctService;

	public void onApplicationEvent(TBillEvent<TbDct> t) {
		TbRec rec = t.getBill().getRec();
		PubAttTmpl attTmpl = this.pubAttTmplFeign.getPubAttTmplByTypeAndGrpId("1", rec.getGrpId());
		if (attTmpl == null) {
			return;
		}
		TbDct dct = (TbDct) t.getBill().getSource();
		AcctBas buyerAcct = this.billAcctService.getAcctBas(dct.getBuyerId(), null, "00");
		CusRec artisan = this.cusRecService.getArtisan(null);
		String content = attTmpl.getContent();
		ServiceFeeDebitDetailListKeyword keyword = new ServiceFeeDebitDetailListKeyword();
		keyword.setArtisanName(artisan.getCusName());
		keyword.setDebitDate(SysDateUtil.parseDate(SysDateUtil.getSystemDate(), "yyyyMMdd", "yyyy��MM��dd��"));
		keyword.setDebitNo("1");
		keyword.setDebitCusName(dct.getBuyerNm());
		keyword.setDebitAcctNo(buyerAcct.getAcctNo());
		keyword.setDebitAmt("��" + MoneyUtil.format(dct.getSerFee()));
		content = ContTemplateHelper.replaceContent(content, keyword);

		String docName = SystemUtil.getUUID();
		String subDir = DateUtil.getSystemDate();
		String localPdfPath = ContConfiguration.getLocalPdfPath();
		if (StringUtil.emptyAndNull(localPdfPath)) {
			throw new UnCheckMsgException("������������������");
		}
		StringBuilder pathBuilder = new StringBuilder(localPdfPath);
		pathBuilder.append(File.separator).append("service-fee").append(File.separator).append(subDir);
		String pdfPath = pathBuilder.toString();
		try {
			PdfUtils.getPdfByContent(content, pdfPath, docName);
		} catch (IOException e) {
			e.printStackTrace();
			throw new UnCheckMsgException("����pdf������" + e.getMessage());
		}
		String localPdfFullPath = FilePathHelper.getFullPath(pdfPath, docName, "pdf");
		PubFile file = this.pubFileService.saveFile(SystemUtil.getUUID(), "������������", localPdfFullPath, "12");
		this.pubFileService.setRelateId(file.getFileId(), rec.getBillId(), file.getFileType());
		if (ChainBillUtil.onCfca()) {
			sign(artisan, localPdfFullPath);
		}
	}

	private void sign(CusRec artisan, String localPdfFullPath) {
		String sealCode = CfcaConfiguration.getCfcaZySealCode();
		PubUser user = this.pubUserFeign.selectByKey(CommonDataUtil.getUserId());
		CfcaSignRequest request = new CfcaSignRequest();
		request.setUserId(user.getUserId());
		request.setUserName(user.getUserName());
		request.setIdNo(user.getCertNo());
		request.setPhoneNo(user.getPhoneNo());
		request.setCusId(artisan.getCusId());
		request.setCusName(artisan.getCusName());
		request.setReason("����������������");
		request.setSrcPdfPath(localPdfFullPath);
		request.setDestPdfPath(localPdfFullPath);
		request.setKey(artisan.getCusName());
		request.setSignPage(Integer.parseInt("0"));
		request.setKeyIndex("2");
		request.setSealCode(sealCode);
		BaseResponse response = this.cfcaSignTxService.sign(request);
		if (!response.isSuccess()) {
			throw new UnCheckMsgException("����������������������cfca��" + response.getMessage());
		}
	}
}
